package com.probiz.estore.imports.handler.catalog.productCategory;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.probiz.estore.catalog.service.ProductCategoryManager;
import com.probiz.estore.common.model.catalog.ProductCategory;
import com.probiz.estore.imports.handler.ColumnHandler;
import com.probiz.estore.imports.handler.basic.ColumnBasicHandler;
import com.probiz.estore.imports.model.Column;
import com.probiz.estore.imports.model.ImportModel;

public class CategoryNameHandler extends ColumnBasicHandler implements ColumnHandler{
	private Logger logger = Logger.getLogger(CategoryNameHandler.class);
	private ProductCategoryManager	productCategoryManager	= null;
	
	public void setProductCategoryManager(ProductCategoryManager productCategoryManager) {
		this.productCategoryManager = productCategoryManager;
	}

	public void setProperty(ImportModel importModel, Column column)	throws Exception {
		ProductCategory category=(ProductCategory)importModel.getTarget();
		String value = column.getValue();
		if(StringUtils.isBlank(value)){
			//设置结果
			importModel.setResult(ImportModel.RESULT_ERROR);
			String msg = "目录名称为空，无法导入";
			logger.error(msg);
			importModel.setMsg(msg);
			return;
		}
		value = value.trim();
		List<ProductCategory> productCategoryList = productCategoryManager.getCategoryByNameAndParent(value, category.getParentId());
		if(0 == productCategoryList.size()){
			//category = new ProductCategory();
			importModel.setUpdate(false);
		}else if(1 == productCategoryList.size()){
			category = productCategoryList.get(0);
			importModel.setUpdate(true);
		}else{
			//设置结果
			importModel.setResult(ImportModel.RESULT_ERROR);
			String msg = "找到多个相同目录名称的目录，不能确定唯一，无法导入";
			logger.error(msg);
			importModel.setMsg(msg);
			return;
		}
		category.setCategoryName(value);
		//设置target
		importModel.setTarget(category);
		//设置结果
		importModel.setResult(ImportModel.RESULT_SUCCESS);
	}
	

}
